﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace FileRenameToolsV2
{
    // 视图模型基类（实现属性通知）
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // 主视图模型（管理多个数据源）
    public class FNViewModel : ViewModelBase
    {
        // 数据源1：用户列表（供DataGrid使用）
        public ObservableCollection<TFileInfo> SourFiles { get; }=new ObservableCollection<TFileInfo>();
        
        public ICollectionView SourFilesView { get; }
        // 数据源2：产品列表（供其他列表控件使用）
        public ObservableCollection<TFileInfo> DestFiles { get; } = new ObservableCollection<TFileInfo>();

        public FNViewModel()
        {
            SourFilesView= CollectionViewSource.GetDefaultView(SourFiles);
            SourFiles.Add(new TFileInfo { FileName = "textFile1", ExtName = ".jpg", FileNDirects = @"d:/tempA" });
            SourFiles.Add(new TFileInfo { FileName = "textFile2", ExtName = ".jpg", FileNDirects = @"d:/tempB" });
            SourFiles.Add(new TFileInfo { FileName = "textFile3", ExtName = ".jpg", FileNDirects = @"d:/tempC" });
        }

        public bool isExists(ObservableCollection<TFileInfo> tDestList, TFileInfo tFileInfo)
        {
            if (tDestList != null && tDestList.Count > 0 && tFileInfo != null)
                foreach (TFileInfo tFile in tDestList)
                    if (tFile.FileName.Equals(tFileInfo.FileName) &&
                        tFile.ExtName.Equals(tFileInfo.ExtName) &&
                        tFile.FileNDirects.Equals(tFileInfo.FileNDirects))
                        return tDestList.Contains(tFileInfo);
            return false;
        }

        public int AddSourFile(TFileInfo tFileInfo)
        {
            if (tFileInfo == null)
                return -1;
            if (isExists(SourFiles, tFileInfo))
                return -2;
            SourFiles.Add(tFileInfo);
            return SourFiles.Count;
        }

        public int AddDestFile(TFileInfo tFileInfo)
        {
            if (tFileInfo == null)
                return -1;
            if (isExists(DestFiles, tFileInfo))
                return -2;
            DestFiles.Add(tFileInfo);
            return DestFiles.Count;
        }
    }
    // 数据模型：用户
    public class TFileInfo
    {
        [DisplayName("文件名")]
        public string FileName { get; set; }
        public string ExtName { get; set; }
        public string FileNDirects { get; set; }

        public bool Equals(TFileInfo destFileInfo)
        {
            return (destFileInfo != null) &&
                FileName.Equals(destFileInfo.FileName) &&
                ExtName.Equals(destFileInfo.ExtName) &&
                FileNDirects.Equals(destFileInfo.FileNDirects);           
        }
    }


}
